package com.smt.modules.app.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.smt.common.enums.Code;
import com.smt.common.utils.EnumToKeyUtil;
import com.smt.common.utils.StringToListUtil;
import com.smt.modules.app.annotation.Login;
import com.smt.modules.app.annotation.LoginUser;
import com.smt.modules.bus.entity.MemberUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import com.smt.common.base.BaseController;
import com.smt.modules.bus.vo.ScheduleVo;
import com.smt.modules.bus.entity.ScheduleEntity;
import com.smt.modules.bus.service.ScheduleService;
import com.smt.common.utils.PageUtils;
import com.smt.common.utils.R;



/**
 * 日程安排表
 *
 * @author murong
 * @email 981620741@qq.com
 * @date 2018-09-29 15:37:08
 */
@RestController
@RequestMapping("app/schedule")
@Api(tags = {"AppScheduleController"},description = "日程安排表(客户端接口)")
public class AppScheduleController extends BaseController{
    @Autowired
    private ScheduleService scheduleService;

    /**
    * 查询日程安排表分页方法
    * @param params
    * @return
    */
    @PostMapping("/list")
    @ApiOperation(value = "/list",notes = "根据条件查询",response = ScheduleVo.class)
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = scheduleService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 根据id获取日程详细信息
     */
    @GetMapping("/eventInfo/{id}")
    @ApiOperation(value ="根据id获取日程详细信息",httpMethod = "GET")
    public R info(@PathVariable("id") Long id){
        return scheduleService.info(id);
    }

    /**
     * 新建日程
     */
    @Login
    @PostMapping("/save")
    @ApiOperation(value  = "新建日程")
    public R save(@RequestBody ScheduleVo schedule,@RequestAttribute("userId") Long userId){
        return scheduleService.save(schedule,userId);
    }

    /**
     * 修改
     */
    @Login
    @PostMapping("/update")
    @ApiOperation(value = "/update",notes = "更新信息保存",response = ScheduleVo.class)
    public R update(@RequestBody ScheduleVo schedule,@RequestAttribute("userId")Long userId){
        return scheduleService.updateEvent(schedule,userId);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{sId}")
    @ApiOperation(value  = "批量删除日程信息")
    public R delete(@PathVariable("sId")Long sId){
        ScheduleEntity scheduleEntity = new ScheduleEntity();
        scheduleEntity.setId(sId);
        scheduleEntity.setPrior(-1);
        boolean b = scheduleService.updateById(scheduleEntity);
        return b?R.ok("批量删除日程信息成功"):
                R.error(EnumToKeyUtil.toInt(Code.DELETE_DATA_ERROR),"批量删除日程信息失败");
    }

    /**
     * 筛选多个人员日程
     *
     * @param projectId
     * @param userStr
     */
    @PostMapping("/memberSchedules/{projectId}/{userIds}/{date}")
    @ApiOperation(value = "筛选多个人员日程",httpMethod = "POST")
    public R memberSchedules(@PathVariable("projectId")Long projectId,
                             @PathVariable("userIds") String userStr,
                             @PathVariable("date")String date){
        StringToListUtil<Long> users = new StringToListUtil<>();
        List<Long> userIds = users.encode(Long.class,userStr);
        R scheduleVos =
                scheduleService.memberSchedules(userIds,projectId,date);
        return scheduleVos;
    }

    /**
     * 删除人员日程
     *
     * @param id
     */
    @Login
    @DeleteMapping("/deleteSchedules/{id}")
    @ApiOperation(value = "删除人员日程",notes = "删除人员日程（假删除）")
    public R deleteSchedules(@PathVariable("id")Long id ,@RequestAttribute("userId")Long userId){
        R scheduleVos =
                scheduleService.deleteSchedules(id,userId);
        return scheduleVos;
    }
}
